#!/usr/bin/python
# -*- coding: utf-8 -*-

import re
import time
import MySQLdb
from math import log

class Video:
    # key: video_id
    # value: baike_tag->tfidf, pop_score, vector_len2
    pass

class User:
    # key: user_id
    # value: baike_tag->tfidf, vector_len22
    pass

videos = {}
users = {}

# 读取今日推荐列表中视频流行分
pop_score_file = open('/home/chaojiansong/videobook/notinsvn/video_score_list.uniq', 'r')
cnt = 0
playlink = {}
imglink = {}
title = {}
duration = {}
####
#得到在json中的原行号
####
video_id_no = {}
video_no = 0

for line in pop_score_file:
    line_id = cnt % 6
    if line_id == 0:
        video_id = line.rstrip()
        videos[video_id] = Video()
        videos[video_id].tfidf = {}
        video_id_no[video_id] = video_no
        video_no = video_no + 1
    elif line_id == 1:
        score = float(line.rstrip())
        videos[video_id].pop_score = score
    elif line_id == 2:
        playlink[video_id] = line.rstrip()
    elif line_id == 3:
        imglink[video_id] = line.rstrip()
    elif line_id == 4:
        title[video_id] = line.rstrip()
    else:
        duration[video_id] = line.rstrip()
    cnt = cnt + 1
pop_score_file.close()

# 读取baike tag df
df = {}
tag_df_file = open('/home/chaojiansong/videobook/notinsvn/baike_tag_df', 'r')
cnt = 0
for line in tag_df_file:
    if cnt % 2 == 0:
        baike_tag = line.rstrip()
    else:
        df[baike_tag] = (int)(line.rstrip())
    cnt = cnt + 1
tag_df_file.close()

# 得到总文档(视频)数
conn = MySQLdb.Connection("172.16.7.58", "root", "researchsucks", "X", 3306)
conn.set_character_set("utf8");
cur = conn.cursor()
cur.execute("select count(distinct video_id) from pop_history_baike_tags")
record = cur.fetchone()
video_cnt = (float)(record[0])

# 读取用户tf, 计算tfidf
user_tf_file = open('/home/chaojiansong/videobook/notinsvn/user_tfvector', 'r')
for line in user_tf_file:
    m = re.match('user: (.*)', line)
    if m:
        user_id = m.group(1)
        users[user_id] = User()
        users[user_id].vector_len2 = 0.0
        users[user_id].tfidf = {}
    else:
        m = re.match('tag: (.*)', line)
        if m:
            tag = m.group(1)
        else:
            m = re.match('tf: (.*)', line)
            if m:
                tf = (int)(m.group(1))
                tfidf = tf * log(video_cnt / df[tag])
                users[user_id].tfidf[tag] = tfidf
                users[user_id].vector_len2 = users[user_id].vector_len2 + tfidf * tfidf
            else:
                print 'regex error'
user_tf_file.close()

# 读取视频tf, 计算tfidf
video_tf_file = open('/home/chaojiansong/videobook/notinsvn/video_tfvector', 'r')
for line in video_tf_file:
    m = re.match('video: (.*)', line)
    if m:
        video_id = m.group(1)
        videos[video_id].vector_len2 = 0.0
    else:
        m = re.match('tag: (.*)', line)
        if m:
            tag = m.group(1)
        else:
            m = re.match('tf: (.*)', line)
            if m:
                tf = (int)(m.group(1))
                tfidf = tf * log(video_cnt / df[tag])
                videos[video_id].tfidf[tag] = tfidf
                videos[video_id].vector_len2 = videos[video_id].vector_len2 + tfidf * tfidf
            else:
                print 'regex error'
video_tf_file.close()

# 个性化推荐列表排序
def get_cos_dis(user_vector, video_vector, user_vector_len, video_vector_len):
    ret = 0.0
    eps = 1e-10
    for tag in user_vector:
        if tag in video_vector:
            ret = ret + user_vector[tag] * video_vector[tag]
    if ret < eps and ret > -eps:
        return 0
    return ret / (user_vector_len * video_vector_len)**0.5

def get_rec_score(user, video):
    pop_score = video.pop_score
    # 分为两张表，分别对个性和流行度排序
    pop_score = 1
    cos_dis = get_cos_dis(user.tfidf, video.tfidf, user.vector_len2, video.vector_len2)
    return pop_score * cos_dis

ISOTIMEFORMAT='%Y-%m-%d'
cur_time = time.strftime(ISOTIMEFORMAT, time.localtime(time.time()))

for user_id in users:
    print 'user_id: ' + user_id
    json = '"images":['
    json_no = ''
    first = True
    video_rec_score = {}
    for video_id in videos:
        video_rec_score[video_id] = get_rec_score(users[user_id], videos[video_id])
    for video_id, rec_score in sorted(video_rec_score.items(), key = lambda x: x[1], reverse = True):
        print 'video_id: ' + video_id
        print 'score: ' + (str)(rec_score)
        print 'playlink: ' + playlink[video_id]
        print 'imglink: ' + imglink[video_id]
        print 'title: ' + title[video_id]
        print 'duration: ' + duration[video_id]
        print 'lineno: ' + str(video_id_no[video_id])
    
        cur_record = ''
        if not first:
        	cur_record = ','
        first = False
        cur_record = cur_record + '{"src":"' + playlink[video_id] + '", "title":"' + re.sub("'", "\\'", title[video_id]) + '", "href":"' + video_id + '", "playlink":"' + playlink[video_id] + '", "duration":"' + duration[video_id] + '"}'
        json = json + cur_record
        json_no = json_no + str(video_id_no[video_id]) + ' '
    json = json + ']}'
    cur.execute("replace into user_rec_list(user_id, time, json) values('" + user_id + "','" + cur_time + "','" + json_no + "')")
    
cur.close()
conn.close()
